﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Business.Entity;
using System.Data.SqlClient;
using System.Data;

namespace Business.Dal
{
    public class MahalleProvider : Provider<Mahalleler>
    {
        public override bool Insert(Mahalleler tObject)
        {
            bool returnValue = false;
            SqlCommand cmd = new SqlCommand("MahalleEkle");
            cmd.Parameters.Add("@IlceID", SqlDbType.Int).Value = tObject.IlceID;
            cmd.Parameters.Add("@MahalleAdi", SqlDbType.NVarChar).Value = tObject.MahalleAdi;
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Connection = Tools.Connection();
            Tools.Open();
            tObject.MahalleID = Convert.ToInt32(cmd.ExecuteScalar());
            
            returnValue = true;
            return returnValue;


        }

        public override bool Update(Mahalleler tObject)
        {
            bool returnValue = false;

            SqlCommand cmd = new SqlCommand("MahalleGuncelle");
            cmd.Parameters.Add("@MahalleID", SqlDbType.Int).Value = tObject.MahalleID;
            cmd.Parameters.Add("@IlceID", SqlDbType.Int).Value = tObject.IlceID;
            cmd.Parameters.Add("@MahalleAdi", SqlDbType.NVarChar).Value = tObject.MahalleAdi;
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Connection = Tools.Connection();
            Tools.Open();
            
            returnValue = Convert.ToBoolean(cmd.ExecuteNonQuery());
            return returnValue;
        }

        public override bool Delete(int RecordID)
        {
            bool returnValue = false;
            SqlCommand cmd = new SqlCommand("MahalleSil");
            cmd.Parameters.Add("MahalleID", SqlDbType.Int).Value = RecordID;
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Connection = Tools.Connection();
            Tools.Open();
            
            returnValue = Convert.ToBoolean(cmd.ExecuteNonQuery());
            return returnValue;
        }

        public List<Mahalleler> GetAllRows()
        {
            List<Mahalleler> returnValue = new List<Mahalleler>();
            SqlCommand cmd = new SqlCommand("MahallelerGetir");
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Connection = Tools.Connection();
            Tools.Open();
            SqlDataReader dr;

            dr = cmd.ExecuteReader();

            while (dr.Read())
            {
                Mahalleler M = new Mahalleler();
                M.MahalleID = dr["MahalleID"] is DBNull ? 0 : Convert.ToInt32(dr["MahalleID"]);
                M.IlceID = dr["IlceID"] is DBNull ? 0 : Convert.ToInt32(dr["IlceID"]);
                M.MahalleAdi = dr["MahalleAdi"] is DBNull ? String.Empty : dr["MahalleAdi"].ToString();
                returnValue.Add(M);
            }
           
            dr.Dispose();
            return returnValue;
        }

        public List<Mahalleler> MahalleGetir(int IlceID)
        {
            List<Mahalleler> returnValue = new List<Mahalleler>();
            SqlCommand cmd = new SqlCommand("MahalleGetir");
            cmd.Parameters.Add("@IlceID", SqlDbType.Int).Value = IlceID;
            cmd.CommandType=CommandType.StoredProcedure;
            cmd.Connection = Tools.Connection();
            Tools.Open();
            SqlDataReader dr;
            dr = cmd.ExecuteReader();

            while (dr.Read())
            {
                Mahalleler M = new Mahalleler();
                M.MahalleID = dr["MahalleID"] is DBNull ? 0 : Convert.ToInt32(dr["MahalleID"]);
                M.IlceID = dr["IlceID"] is DBNull ? 0 : Convert.ToInt32(dr["IlceID"]);
                M.MahalleAdi = dr["MahalleAdi"] is DBNull ? String.Empty : dr["MahalleAdi"].ToString();
                returnValue.Add(M);
            
            }
            dr.Dispose();
            return returnValue;
        
        }
         public Mahalleler MahalleGetirMahalleID(int MahalleID)
         {
             SqlCommand cmd = new SqlCommand("MahalleGetirMahalleID");
             cmd.Parameters.Add("@MahalleID", SqlDbType.Int).Value = MahalleID;
             cmd.CommandType = CommandType.StoredProcedure;
             cmd.Connection = Tools.Connection();
             Tools.Open();
             SqlDataReader dr = cmd.ExecuteReader();
             dr.Read();

             Mahalleler M = new Mahalleler();
             M.MahalleID = dr["MahalleID"] is DBNull ? 0 : Convert.ToInt32(dr["MahalleID"]);
             M.IlceID = dr["IlceID"] is DBNull ? 0: Convert.ToInt32(dr["IlceID"]);
             M.MahalleAdi = dr["MahalleAdi"] is DBNull ? String.Empty : dr["MahalleAdi"].ToString();

             dr.Dispose();
             return M;
         }

       
    }
}
